import shutil

from pathlib import Path
from random import sample, choice

class MyFileOps:
    def __init__(self, workpath = './tmp',
                       subdir = 'a/b/c/d',
                       dstdir = './dst',
                       header = 'xyz',
                       filequantity = 50):
        self.wp = workpath
        self.sd = subdir
        self.dst = dstdir
        self.header = header
        self.fq = filequantity

    def _random_file_name(self):
        lowercase = 'abcdefghijklmnopqrstuvwxyz'
        filenames = [''.join(sample(lowercase, 4)) for _ in range(self.fq)]
        return filenames

    def _dir_sep(self):
        dirs = list(Path(self.sd).parents)
        dirs.pop()
        dirs.append(Path(self.sd))
        return dirs

    def _create_dir(self):
        fullpath = Path(self.wp) / Path(self.sd)
        fullpath.mkdir(parents=True, exist_ok=True)

    def create_files(self):
        self._create_dir()
        fn = self._random_file_name()
        dirs = self._dir_sep()

        for i in fn:
            (self.wp / choice(dirs) / i).touch()

    def copy_file(self):
        self.create_files()

        def ignore(src, names):
            ig_files = filter(lambda name:name[0] in self.header, names)
            return set(ig_files)
        shutil.copytree(self.wp, self.dst, ignore=ignore)


mycopy = MyFileOps()
mycopy.copy_file()


# 做得不错
